export default {

  requiredInput: (showText, type = 'string') => {
    return { type: type, required: true, message: '请输入' + showText }
  },

  /**
   * 子对象必填
   * @param obj 父对象
   * @param validateObj 子对象
   * @param validateField 子对象字段
   * @param showText 提示文字
   * @param minLen 最小长度
   * @returns 
   */
  requiredSubInput: (obj, validateObj, validateField, showText,minLen=0) => {
    return {
      required: true,
      validator: (_rule, value) => {
        let subObj = obj[validateObj][validateField]
        if (subObj === undefined || subObj === '') {
          return Promise.reject(`请输入${showText}`)
        } else {
          if(minLen && subObj.length<minLen){
            return Promise.reject(`${showText}最小长度${minLen}`)
          }
          return Promise.resolve()
        }
      },
      trigger: 'change',
    }
  },

  requiredSelect: (showText, type = 'string') => {
    return { type: type, required: true, message: '请选择' + showText }
  },

  /**
 * 子对象必填
 * @param obj 父对象
 * @param validateObj 子对象
 * @param validateField 子对象字段
 * @param showText 提示文字
 * @returns 
 */
  requiredSubSelect: (obj, validateObj, validateField, showText) => {
    return {
      required: true,
      validator: (_rule, value) => {
        let subObj = obj[validateObj][validateField]
        if (subObj === undefined || subObj === '') {
          return Promise.reject(`请选择${showText}`)
        } else {
          return Promise.resolve()
        }
      },
      trigger: 'change',
    }
  },

  requiredUpload: (showText, type = 'string') => {
    return { type: type, required: true, message: '请上传' + showText }
  },

  /**
 * 子对象必填
 * @param obj 父对象
 * @param validateObj 子对象
 * @param validateField 子对象字段
 * @param showText 提示文字
 * @returns 
 */
  requiredSubUpload: (obj, validateObj, validateField, showText) => {
    return {
      required: true,
      validator: (_rule, value) => {
        let subObj = obj[validateObj][validateField]
        if (subObj === undefined || subObj === '') {
          return Promise.reject(`请上传${showText}`)
        } else {
          return Promise.resolve()
        }
      },
      trigger: 'change',
    }
  },

  // 金额范围的判断  参数后面的问号，表示： 可选参数 
  amountRange: (showText, minVal = 0.01, maxVal?) => {
    return {
      validator: (rule, value) => {
        if (typeof (minVal) == 'number' && value < minVal) {
          return Promise.reject(`${showText}不能小于${minVal}元`)
        }
        if (typeof (maxVal) == 'number' && value > maxVal) {
          return Promise.reject(`${showText}不能大于${maxVal}元`)
        }
        else {
          return Promise.resolve()
        }

      }
    }
  },

  mobile: { pattern: /^1\d{10}$/, message: '请输入正确的手机号' },

  /**
 * 子对象必填
 * @param obj 父对象
 * @param validateObj 子对象
 * @param validateField 子对象字段
 * @returns 
 */
  subMobile: (obj, validateObj, validateField) => {
    return {
      required: true,
      validator: (_rule, value) => {
        let subObj = obj[validateObj][validateField]
        let reg = /^1\d{10}$/
        if (!reg.test(subObj)) {
          return Promise.reject('请输入正确的手机号')
        } else {
          return Promise.resolve()
        }
      },
      trigger: 'change',
    }
  },

  emall: { pattern: /^.+@.+(\.).+$/, message: '请输入正确的邮箱' },

    /**
 * 子对象必填
 * @param obj 父对象
 * @param validateObj 子对象
 * @param validateField 子对象字段
 * @returns 
 */
    subEmail: (obj, validateObj, validateField) => {
      return {
        required: true,
        validator: (_rule, value) => {
          let subObj = obj[validateObj][validateField]
          let reg = /^.+@.+(\.).+$/
          if (!reg.test(subObj)) {
            return Promise.reject('请输入正确的邮箱')
          } else {
            return Promise.resolve()
          }
        },
        trigger: 'change',
      }
    },

  date: { pattern: /^\d{4}-\d{2}-\d{2}$/, message: '请输入正确的日期[yyyy-MM-dd]' },

  dateOrForever: { pattern: /^长期|(\d{4}-\d{2}-\d{2})$/, message: '请输入正确的日期[yyyy-MM-dd]或选择长期有效' },

  IDCard: [{
    required: true,
    validator: (_rule, value) => {
      if (!value) {
        return Promise.reject('请输入身份证号')
      }
      let _IDRe18 = /^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
      let _IDre15 = /^([1-6][1-9]|50)\d{4}\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}$/
      // 校验身份证：
      if (_IDRe18.test(value) || _IDre15.test(value)) {
        return Promise.resolve()
      } else {
        return Promise.reject('请输入正确的身份证号')
      }
    },
    trigger: 'change',
  }
  ],

  /**
* 子对象必填
* @param obj 父对象
* @param validateObj 子对象
* @param validateField 子对象字段
* @returns 
*/
  subIDCard: (obj, validateObj, validateField) => {
    return {
      required: true,
      validator: (_rule, value) => {
        let subObj = obj[validateObj][validateField]
        if (!subObj) {
          return Promise.reject('请输入身份证号')
        }
        let _IDRe18 = /^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
        let _IDre15 = /^([1-6][1-9]|50)\d{4}\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}$/
        // 校验身份证：
        if (_IDRe18.test(subObj) || _IDre15.test(subObj)) {
          return Promise.resolve()
        } else {
          return Promise.reject('请输入正确的身份证号')
        }
      },
      trigger: 'change',
    }
  },

}